package com.qingxin.wuye.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.service.WyBuildingService;
import com.qingxin.wuye.service.WyCarportAreaService;
import com.qingxin.wuye.service.WyGardenService;
import com.qingxin.wuye.service.WyPropertyCompanyService;


@Controller
@RequestMapping("/menutree")
public class MenuTreeAction extends BaseAction {
	
	@Autowired
	private WyPropertyCompanyService companyService;
	@Autowired
	private WyGardenService gardenService;
	@Autowired
	private WyBuildingService buildingService;
	
	@Autowired
	private WyCarportAreaService carportAreaService;
	
	
	@RequestMapping(value="/menudata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object menudata(String module, String pdatatype, String pid, HttpServletRequest request){
		if("garden".equalsIgnoreCase(module)){
			return loadGarden(pdatatype, pid, request);
		}
		
		if("building".equalsIgnoreCase(module)){
			return loadBuilding(pdatatype, pid, request);
		}
		
		if("carport".equalsIgnoreCase(module)){
			return loadCarport(pdatatype, pid, request);
		}
		
		return loadCommon(module, pdatatype, pid, request);
	}
	
	
	private List<JsTreeNode> loadGarden(String pdatatype, String pid, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			List<JsTreeNode> list = companyService.getCompanyData(loginUser, false);
			return list;
		}
		return new ArrayList<>();
	}
	
	
	private List<JsTreeNode> loadBuilding(String pdatatype, String pid, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = companyService.getCompanyData(loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = gardenService.getUITreeData(pid, loginUser, false);
				return list;
			}
		}else {
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = gardenService.getUITreeData(null, loginUser, false);
				return list;
			}
		}
		return new ArrayList<>();
	}
	
	
	
	private List<JsTreeNode> loadCommon(String module, String pdatatype, String pid, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = companyService.getCompanyData(loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = gardenService.getUITreeData(pid, loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = buildingService.getUITreeData(pid, loginUser, false);
				return list;
			}
		}else {
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = gardenService.getUITreeData(null, loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = buildingService.getUITreeData(pid, loginUser, false);
				return list;
			}
		}
		return new ArrayList<>();
	}
	
	
	
	public List<JsTreeNode> loadCarport(String pdatatype, String pid, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = companyService.getCompanyData(loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = gardenService.getUITreeData(pid, loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = carportAreaService.getUITreeData(pid, loginUser, false);
				return list;
			}
		}else {
			if(StringUtils.isEmpty(pid) || "#".equals(pid)){
				List<JsTreeNode> list = gardenService.getUITreeData(null, loginUser, true);
				return list;
			}
			if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(pdatatype)){
				List<JsTreeNode> list = carportAreaService.getUITreeData(pid, loginUser, false);
				return list;
			}
		}
		return new ArrayList<>();
	}
	
	
}
